package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void downOrUpHandel( String message){
        log.info("接受到文章上下架的消息 {}",message);
        Map map = JSON.parseObject(message, Map.class);
        Object enable = map.get("enable");
        Object articleId = map.get("articleId");

        boolean isDown = enable.equals(1) ? false : true;

        apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId,articleId)
                                                .set(ApArticleConfig::getIsDown,isDown));
        log.info("文章上下架修改完成========");
    }
}
